package com.lvmama.pangolin.service.impl;

import com.lvmama.pangolin.common.StringTools;
import com.lvmama.pangolin.entity.SegVo;
import com.lvmama.pangolin.service.TicketCommodityScoreService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <h1>门票类商品评分实现类</h1>
 * @Author:wangpeng
 */
@Service
public class TicketCommodityScoreServiceImpl implements TicketCommodityScoreService{

    public static final float PRICE_FACTOR=0.3F;//商品价格匹配度影响因子

    public static final float NAME_FACTOR=0.5F;//商品匹配度命名影响因子

    public static final float TYPE_FACTOR=0.2F;//商品匹配度商品分类影响椅子

    /**
     * 命名评分实现
     * @param segName
     * @param msegName
     * @return
     */
    @Override
    public float nameScore(String segName,String msegName) {
        List<SegVo> nameList = null;
        List<SegVo> mnameList = null;
        float score = 0f;//返回值
        int segLen = 0;
        int msegLen = 0;
        int strLen = 0;
        int mstrLen = 0;

        int segLen_match = 0;
        int msegLen_match = 0;
        int strLen_match = 0;
        int mstrLen_match = 0;

        if(StringTools.isValid(segName))
        {
            nameList = new ArrayList<SegVo>();
            String[]  name = segName.split(",");
            for (int i = 0; i < name.length; i++)
            {
                SegVo vo = new SegVo();
                vo.setSeq(i);
                vo.setName(name[i]);
                vo.setMatch(false);
                nameList.add(vo);
                strLen += name[i].length();
                segLen ++;
            }
        }
        if(StringTools.isValid(msegName))
        {
            mnameList = new ArrayList<SegVo>();
            String[]  name = msegName.split(",");
            for (int i = 0; i < name.length; i++)
            {
                SegVo vo = new SegVo();
                vo.setSeq(i);
                vo.setName(name[i]);
                vo.setMatch(false);
                mnameList.add(vo);
                mstrLen += name[i].length();
                msegLen ++;
            }
        }
        if((nameList == null || nameList.isEmpty())
                ||(mnameList == null || mnameList.isEmpty()))
        {
            score = 0f;
        }else
        {
            for (SegVo seg : nameList)
            {
                String name = seg.getName();
                for (SegVo vo : mnameList)
                {
                    if(name.equals(vo.getName()))
                    {
                        if(!vo.isMatch())
                        {
                            seg.setMatch(true);
                            vo.setMatch(true);
                            segLen_match ++;
                            msegLen_match ++;
                            strLen_match += name.length();
                            mstrLen_match += name.length();
                            break;
                        }

                    }
                }
            }
            score = ((0.5f*segLen_match)/(segLen) + (0.5f*strLen_match)/strLen)*0.6f
                    + ((0.5f*msegLen_match)/(msegLen) + (0.5f*mstrLen_match)/mstrLen)*0.4f;
        }
        return score;
    }

    /**
     * 价格评分实现
     * @param basePrice
     * @param price
     * @return
     */
    @Override
    public float priceScore(String basePrice, String price) {
        return 0;
    }

    /**
     * 类型评分实现
     * @param type1
     * @param type2
     * @return
     */
    @Override
    public float tickeTypeScore(String type1, String type2) {
        return 0;
    }


}
